//
//  MOPersonSetViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/6/2.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOPersonSetViewController.h"
#import "MOPersonSetViewCell.h"
#import "MOChangePasswordViewController.h"
#import "MOChangeGesturPasswordController.h"

@interface MOPersonSetViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) UITableView *tableView;

@property(nonatomic,strong) NSMutableArray *dataArray;

//是否设置过手势密码
@property(nonatomic,copy)NSString *isHasSetGesterPassword;

@end

@implementation MOPersonSetViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self requestData];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self creatView];
    
    [self initData];

}


-(void)creatView
{
    self.title = @"个人设置";
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.tableView];
}

-(void)initData
{
    self.dataArray = [NSMutableArray array];
    NSArray *arr = @[@"密码修改",@"手势密码：开启",@"修改手势密码"];
    for (int i = 0; i<1; i++) {
        MOPersonSetModel *model = [[MOPersonSetModel alloc]init];
        if (i==1) {
            model.type = @"1";
            model.isOpen = @"1";
        }else{
            model.type = @"0";
            model.isOpen = @"0";
        }
        model.nameStr = arr[i];
        [self.dataArray addObject:model];
    }
    [self.tableView reloadData];
}

-(void)requestData
{
    MOUser *user = [MOUser sharedMOUser];

    NSString *postUrl = [NSString stringWithFormat:@"%@/%@?authorization=%@",FINDLOCKERBYUSER_URL,user.userName,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [PPNetworkHelper GET:postUrl parameters:@{} success:^(id responseObject) {
        NSLog(@"查询手势密码:%@",responseObject);
        if ([responseObject[@"enable"] integerValue]==1) {//0：关闭 1：开启
            
            [MOUser updateStringValue:@"1" forKey:@"USER_NEW_HAVEGESTUREPASSWORD"];
            [self savePassWord:responseObject[@"locker"]];
            self.isHasSetGesterPassword = @"1";
            
            if ([user.openGesturePassword integerValue]==0) {//默认
                [self yesGestureP];
            }else if ([user.openGesturePassword integerValue]==1) {//关闭了手势密码
                [self yesButNoOpenGestureP];
            }else{//打开了手势密码
                [self yesGestureP];
            }
        
        }else{
            [self noGestureP];
        }
    } failure:^(NSError *error) {
        [self noGestureP];
    }];
}

#pragma mark -- 设置并打开了手势密码
-(void)yesGestureP
{
    self.dataArray = [NSMutableArray array];
    NSArray *arr = @[@"密码修改",@"手势密码：开启",@"修改手势密码"];
    for (int i = 0; i<3; i++) {
        MOPersonSetModel *model = [[MOPersonSetModel alloc]init];
        if (i==1) {
            model.type = @"1";
            model.isOpen = @"1";
        }else{
            model.type = @"0";
            model.isOpen = @"0";
        }
        model.nameStr = arr[i];
        [self.dataArray addObject:model];
        
    }
    [self.tableView reloadData];
}

#pragma mark -- 设置但没有打开手势密码
-(void)yesButNoOpenGestureP
{
    self.dataArray = [NSMutableArray array];
    NSArray *arr = @[@"密码修改",@"手势密码：关闭",@"修改手势密码"];
    for (int i = 0; i<3; i++) {
        MOPersonSetModel *model = [[MOPersonSetModel alloc]init];
        if (i==0) {
            model.type = @"0";
            model.isOpen = @"0";
        }else if (i==1){
            model.type = @"1";
            model.isOpen = @"0";
        }else{
            model.type = @"0";
            model.isOpen = @"0";
        }
        model.nameStr = arr[i];
        [self.dataArray addObject:model];
        
    }
    [self.tableView reloadData];
}

#pragma mark -- 没有设置手势密码
-(void)noGestureP
{
    self.isHasSetGesterPassword = @"0";
    self.dataArray = [NSMutableArray array];
    NSArray *arr = @[@"密码修改",@"手势密码：关闭",@"修改手势密码"];
    for (int i = 0; i<2; i++) {
        MOPersonSetModel *model = [[MOPersonSetModel alloc]init];
        if (i==1) {
            model.type = @"1";
            model.isOpen = @"0";
        }else{
            model.type = @"0";
            model.isOpen = @"0";
        }
        model.nameStr = arr[i];
        [self.dataArray addObject:model];
    }
    [self.tableView reloadData];
}


- (void)savePassWord:(NSString *)password
{
    NSUserDefaults *userdefault = [NSUserDefaults standardUserDefaults];
    NSString *enPassword = [password entryptAESBase64];
    [userdefault setObject:@{@"password":enPassword} forKey:GESTUREPASSWORD];
    [userdefault setObject:password forKey:@"GESTURESETPASSWORD"];
    [userdefault synchronize];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    __weak typeof (self)weakSelf = self;
    MOPersonSetViewCell *setCell = [MOPersonSetViewCell cellWithTableView:tableView andIndexPath:indexPath];
    setCell.clickSwitchBlock = ^(NSString * _Nonnull isOpen) {
        
        if ([weakSelf.isHasSetGesterPassword integerValue]==1) {//已经设置过密码
            if ([isOpen integerValue]==1) {
                [MOUser updateStringValue:@"2" forKey:@"USER_NEW_OPENGESTUREPASSWORD"];
                [self yesGestureP];
            }else{
                [MOUser updateStringValue:@"1" forKey:@"USER_NEW_OPENGESTUREPASSWORD"];
                [self yesButNoOpenGestureP];
            }
        }else{
            if ([isOpen integerValue]==1) {
                MOChangeGesturPasswordController *vc = [[MOChangeGesturPasswordController alloc]init];
                vc.status = GesturePasswordStatusSet;
                [self.navigationController pushViewController:vc animated:YES];
            }
        }
    };
    setCell.model = self.dataArray[indexPath.section];
    return setCell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kHConversions(54);
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 2) {
        return 0.5;
    }
    else {
        return 10;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    return 0.0000001;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return nil;
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        MOChangePasswordViewController *vc = [[MOChangePasswordViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.section==2){
        MOChangeGesturPasswordController *vc = [[MOChangeGesturPasswordController alloc]init];
        vc.status = GesturePasswordStatusReset;
        [self.navigationController pushViewController:vc animated:YES];
    }
}


#pragma mark -- 懒加载
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarAndNavigationBarHeight-TabbarSafeBottomMargin) style:UITableViewStyleGrouped];
        _tableView.separatorColor = KGrayColor_f8f8f8;
        _tableView.backgroundColor = KGrayColor_f8f8f8;
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    return _tableView;
}


@end
